// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ExamServiceImp.java

package com.iphaeton.lms.business.exam;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.persistence.spring.JdbcPService;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;

// Referenced classes of package com.iphaeton.lms.business.exam:
//            IExamService

public class ExamServiceImp extends PersistentServiceSupport implements
		IExamService {
	private static Logger logger;
	Calendar cal;
	Date time;
	DateFormat df;
	String currentTime;

	static {
		logger = Logger.getLogger(com.iphaeton.lms.business.exam.ExamServiceImp.class
						.getName());
	}
	

	public ExamServiceImp() {
		cal = Calendar.getInstance();
		time = cal.getTime();
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		currentTime = df.format(time);
	}

	public List queryCurrentExam(String user_id, String organize_url)
			throws BusinessException {
		final String userId = user_id;
		final String orgUrl = organize_url;
		final String current_time = currentTime;
		List list = null;
		CallableStatementCallback cb = new CallableStatementCallback() {

			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				List list = new LinkedList();
				cs.setString(1, userId);
				cs.setString(2, orgUrl);
				cs.setString(3, current_time);
				cs.execute();
				ResultSet rs;
				Map map;
				for (rs = cs.getResultSet(); rs.next(); list.add(map)) {
					map = new HashMap();
					map.put("id", rs.getString("id"));
					map.put("name", rs.getString("name"));
					map.put("type", rs.getString("type"));
					map.put("sort", rs.getString("sort"));
					map.put("score", rs.getString("score"));
					map.put("period", rs.getString("period"));
					map.put("start_time", rs.getString("start_time"));
					map.put("end_time", rs.getString("end_time"));
					try {
						if (df.parse(rs.getString("start_time")).before(time)
								&& time.before(df.parse(rs
										.getString("end_time"))))
							map.put("state", "1");
						else if (time.before(df.parse(rs
								.getString("start_time"))))
							map.put("state", "2");
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}

				rs.close();
				return list;
			}

		};
		try {
			list = (List) jdbcpservice.getSpringJdbcTemplate().execute(
					"{call pro_student_exam(?,?,?)}", cb);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u67E5\u8BE2\u5B66\u5458\u8003\u8BD5\u65F6\u51FA\u9519: "
							+ e.getMessage());
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u67E5\u8BE2\u5B66\u5458\u8003\u8BD5\u65F6\u51FA\u9519: "
							+ e.getMessage());
		}
		return list;
	}

	public List queryExamQuestion(String paper_id) throws BusinessException {
		final String paperId = paper_id;
		List list = null;
		CallableStatementCallback cb = new CallableStatementCallback() {

			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				List list = new LinkedList();
				cs.setString(1, paperId);
				cs.execute();
				ResultSet rs;
				Map map;
				for (rs = cs.getResultSet(); rs.next(); list.add(map)) {
					map = new HashMap();
					map.put("id", rs.getString("id"));
					map.put("question", rs.getString("question"));
					map.put("sname", rs.getString("sname"));
					map.put("prepare_answer", rs.getString("prepare_answer"));
					map.put("score", rs.getString("score"));
				}

				rs.close();
				return list;
			}

		};
		try {
			list = (List) jdbcpservice.getSpringJdbcTemplate().execute(
					"{call pro_paper_question(?)}", cb);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u67E5\u8BE2\u8BD5\u5377\u8BD5\u9898\u5217\u8868\u65F6\u51FA\u9519: "
							+ e.getMessage());
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u67E5\u8BE2\u8BD5\u5377\u8BD5\u9898\u5217\u8868\u65F6\u51FA\u9519: "
							+ e.getMessage());
		}
		return list;
	}

	public void studentAnswer(String paper_id, String user_id,
			String question_id, String user_answer) throws BusinessException {
		final String paperId = paper_id;
		final String userId = user_id;
		final String questionId = question_id;
		final String userAnswer = user_answer;
		CallableStatementCallback cb = new CallableStatementCallback() {

			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				cs.setString(1, paperId);
				cs.setString(2, userId);
				cs.setString(3, questionId);
				cs.setString(4, userAnswer);
				cs.execute();
				return null;
			}

		};
		try {
			jdbcpservice.getSpringJdbcTemplate().execute(
					"{call pro_student_answer(?,?,?,?)}", cb);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7528\u6237\u63D0\u4EA4\u8BD5\u5377\u7B54\u6848\u65F6\u51FA\u9519: "
							+ e.getMessage());
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7528\u6237\u63D0\u4EA4\u8BD5\u5377\u7B54\u6848\u65F6\u51FA\u9519: "
							+ e.getMessage());
		}
	}

	public List paperMark(String paper_id, String user_id)
			throws BusinessException {
		final String paperId = paper_id;
		final String userId = user_id;
		List list = null;
		CallableStatementCallback cb = new CallableStatementCallback() {

			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				List list = new LinkedList();
				cs.setString(1, paperId);
				cs.setString(2, userId);
				cs.execute();
				ResultSet rs;
				Map map;
				for (rs = cs.getResultSet(); rs.next(); list.add(map)) {
					map = new HashMap();
					map.put("id", rs.getString("id"));
					map.put("question", rs.getString("question"));
					map.put("sname", rs.getString("sname"));
					map.put("prepare_answer", rs.getString("prepare_answer"));
					map.put("standard_answer", rs.getString("standard_answer"));
					map.put("user_answer", rs.getString("user_answer"));
					map.put("score", rs.getString("question_score"));
				}

				rs.close();
				return list;
			}

		};
		try {
			list = (List) jdbcpservice.getSpringJdbcTemplate().execute(
					"{call pro_paper_mark(?,?)}", cb);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u6279\u9605\u7528\u6237\u7B54\u5377\u65F6\u51FA\u9519: "
							+ e.getMessage());
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u6279\u9605\u7528\u6237\u7B54\u5377\u65F6\u51FA\u9519: "
							+ e.getMessage());
		}
		return list;
	}

	public void setQuestionScore(String paper_id, String user_id,
			String question_id, float score) throws BusinessException {
		String sql = "update t_user_answer set score='" + score
				+ "' where paper_id='" + paper_id + "' and user_id='" + user_id
				+ "' and question_id='" + question_id + "'";
		try {
			jdbcpservice.execute(sql);
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7ED9\u7528\u6237\u8003\u8BD5\u8BD5\u5377\u8BD5\u9898\u6253\u5206\u65F6\u51FA\u9519: "
							+ e.getMessage() + "!");
		}
	}

	public void setPaperScore(String paper_id, String user_id, float score)
			throws BusinessException {
		String sql = "update t_user_score set score='" + score
				+ "',state=1 where paper_id='" + paper_id + "' and user_id='"
				+ user_id + "'";
		String countSql = "select count (user_id) as count from t_user_score where paper_id='"
				+ paper_id + "' and state=2";
		String stateSql = "update t_paper set state=1 where id='" + paper_id
				+ "'";
		try {
			jdbcpservice.execute(sql);
			List list = jdbcpservice.query(countSql);
			if (list != null
					&& ((Integer) ((Map) list.get(0)).get("count")).intValue() == 0)
				jdbcpservice.execute(stateSql);
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7ED9\u7528\u6237\u8003\u8BD5\u8BD5\u5377\u6253\u5206\u65F6\u51FA\u9519: "
							+ e.getMessage() + "!");
		}
	}

	public void startExam(String paper_id, String user_id, String exam_date)
			throws BusinessException {
		if (!started(paper_id, user_id)) {
			final String paperId = paper_id;
			final String userId = user_id;
			final String examDate = exam_date;
			CallableStatementCallback cb = new CallableStatementCallback() {

				public Object doInCallableStatement(CallableStatement cs)
						throws SQLException, DataAccessException {
					cs.setString(1, paperId);
					cs.setString(2, userId);
					cs.setString(3, "answer_address");
					cs.setFloat(4, 0.0F);
					cs.setInt(5, 2);
					cs.setString(6, examDate);
					cs.execute();
					return null;
				}

			};
			try {
				jdbcpservice.getSpringJdbcTemplate().execute(
						"{call pro_exam_start(?,?,?,?,?,?)}", cb);
			} catch (DataAccessException e) {
				e.printStackTrace();
				throw new BusinessException(
						"\u8BBE\u7F6E\u7528\u6237\u5F00\u59CB\u7B54\u5377\u72B6\u6001\u51FA\u9519: "
								+ e.getMessage());
			} catch (PersistenceException e) {
				e.printStackTrace();
				throw new BusinessException(
						"\u8BBE\u7F6E\u7528\u6237\u5F00\u59CB\u7B54\u5377\u72B6\u6001\u51FA\u9519: "
								+ e.getMessage());
			}
		}
	}

	public List myPaper(String paper_id, String user_id)
			throws BusinessException {
		final String paperId = paper_id;
		final String userId = user_id;
		List list = null;
		CallableStatementCallback cb = new CallableStatementCallback() {

			public Object doInCallableStatement(CallableStatement cs)
					throws SQLException, DataAccessException {
				List list = new LinkedList();
				cs.setString(1, paperId);
				cs.setString(2, userId);
				cs.execute();
				ResultSet rs;
				Map map;
				for (rs = cs.getResultSet(); rs.next(); list.add(map)) {
					map = new HashMap();
					map.put("id", rs.getString("id"));
					map.put("question", rs.getString("question"));
					map.put("sname", rs.getString("sname"));
					map.put("prepare_answer", rs.getString("prepare_answer"));
					map.put("standard_answer", rs.getString("standard_answer"));
					map.put("user_answer", rs.getString("user_answer"));
					map.put("question_score", rs.getString("question_score"));
					map.put("user_score", rs.getString("user_score"));
					map.put("total_score", rs.getString("total_score"));
				}

				rs.close();
				return list;
			}

		};
		try {
			list = (List) jdbcpservice.getSpringJdbcTemplate().execute(
					"{call pro_paper_mark(?,?)}", cb);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7528\u6237\u67E5\u770B\u7B54\u5377\u8BE6\u7EC6\u5F97\u5206\u60C5\u51B5\u65F6\u51FA\u9519: "
							+ e.getMessage());
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u7528\u6237\u67E5\u770B\u7B54\u5377\u8BE6\u7EC6\u5F97\u5206\u60C5\u51B5\u65F6\u51FA\u9519: "
							+ e.getMessage());
		}
		return list;
	}

	private boolean started(String paper_id, String user_id)
			throws BusinessException {
		boolean flag = false;
		String sql = "select * from t_user_score where user_id='" + user_id
				+ "' and paper_id='" + paper_id + "'";
		List list;
		try {
			list = jdbcpservice.query(sql);
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw new BusinessException(
					"\u9A8C\u8BC1\u8003\u8BD5\u662F\u5426\u5F00\u59CB\u65F6\u51FA\u9519!");
		}
		if (list != null && list.size() > 0)
			flag = true;
		return flag;
	}

	public List findOrganizeExam(String id, String sort, String currentTime,
			String start, String end) throws BusinessException {
		List list = null;
		return list;
	}

	
}
